﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebRole1
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        dbManager db = new dbManager();
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            TextBox1.Enabled = false;
            TextBox2.Enabled = false;
            TextBox3.Enabled = false;
            TextBox4.Enabled = false;
            string email = TextBox1.Text;
            string name = TextBox2.Text;
            string password = TextBox3.Text;
            if (db.emailExist(email))
            {
                Label1.Text = "Email has been registered before";
                TextBox1.Enabled = true;
                TextBox2.Enabled = true;
                TextBox3.Enabled = true;
                TextBox4.Enabled = true;
                TextBox3.Text = "";
                TextBox4.Text = "";
            }
            else
            {
                bool result = db.createUser(email, password, name);
                result = db.addRole("user", email);
                if (result == true)
                {
                    Label1.Text = "You have been registered. Click <a href='login.aspx'> here </a href> to continue";
                }
                else
                {
                    Label1.Text = "Register Fail. Please try again.";
                    TextBox1.Enabled = true;
                    TextBox2.Enabled = true;
                    TextBox3.Enabled = true;
                    TextBox4.Enabled = true;
                    TextBox3.Text = "";
                    TextBox4.Text = "";
                }
            }
            Label1.Visible = true;

        }
    }
}